/*
 * @Descripttion:
 * @version:
 * @Author: 十三
 * @Date: 2022-12-06 17:04:43
 * @LastEditors: 十三
 * @LastEditTime: 2022-12-21 20:51:24
 */
import { defineStore } from 'pinia';

import { getSystemWebsite } from '@/api/system';
import { IWebsite } from '@/api/system/type';

export const useSystemStore = defineStore('SYSTEM', {
  state: () => {
    return {
      theme: 'light',
      website: {} as IWebsite.Data
    };
  },
  actions: {
    settingTheme(theme: string) {
      this.theme = theme;
    },
    /**
         * 获取站点配置
         */
    getWebsite() {
      return new Promise<IWebsite.Data>((resolve, reject) => {
        if (Object.keys(this.website).length > 0) {
          resolve(this.website);
        } else {
          getSystemWebsite().then(res => {
            this.setWebsite(res);
            resolve(res);
          }).catch(error => {
            reject(error);
          });
        }
      });
    },
    setWebsite(v: IWebsite.Data) {
      this.website = { ...v };
    }
  },
  // 使用该插件，开启数据缓存
  persist: {
    // 这里存储默认使用的是session
    enabled: true,
    strategies: [
      {
        // key的名称
        key: 'SYSTEM',
        // 更改默认存储，我更改为localStorage
        storage: localStorage
        // 默认是全部进去存储, 可以选择哪些进入local存储，这样就不用全部都进去存储了
      }
    ]
  }
});
